<?php

require_once('local_config.php');

//The one (and only) connection
$conn = NULL;

//DB connection stuff
function dbConnect() {
    //DB Info
    global $dbhost;
    global $dbuser;
    global $dbpass;
    global $db_name;

    global $conn;
    if ( $conn == NULL ) {
        $conn = mysql_connect($dbhost, $dbuser, $dbpass) or die(mysql_error());
        mysql_select_db($db_name);
    }
}

function dbClose() {
    global $conn;
    if ( $conn != NULL ) {
        mysql_close($conn);
    }
    $conn = NULL;
}

//Images CRUD stuff

//Returns a row by its id or false
function dbGetImageById($id) {
    global $conn;
    dbConnect();

    if ( $result = mysql_query("select image from images where id = '" . $id . "'", $conn) or die(mysql_error()) ){
        if ( mysql_num_rows($result) ) {
            $row = mysql_fetch_array( $result );
            return $row;
        }
    }

    return false;
}

//Returns a row (array) or false
function dbGetImageInfoById($id) {
    global $conn;
    dbConnect();

    if ( $result = mysql_query("select id, description, submit_date, source, email, status, publish_date, image, comment from images where id = '" . $id . "'", $conn) or die(mysql_error()) ){
        if ( mysql_num_rows($result) ) {
            $row = mysql_fetch_array( $result );
            return $row;
        }
    }

    return false;
}

//Returns a row (array) or false
function dbGetImageInfoByPublishDate($publishDate) {
    global $conn;
    dbConnect();

    if ( $result = mysql_query("select id, description, submit_date, source, email, status, publish_date, image, comment from images where publish_date = '" . $publishDate . "' and status = 2", $conn) or die(mysql_error()) ){
        if ( mysql_num_rows($result) ) {
            $row = mysql_fetch_array( $result );
            return $row;
        }
    }

    return false;
}

//Returns a list of rows containing all image infos
function dbGetImageInfos() {
    global $conn;
    dbConnect();
    
    $rows = array();
    if ( $result = mysql_query("select id, description, submit_date, source, email, status, publish_date, comment from images order by status, publish_date", $conn) or die(mysql_error()) ){
        for ( $i = 0; $i < mysql_num_rows($result); $i++ ){
            $row = mysql_fetch_array( $result );
            $rows[$i] = $row;
        }
    }

    return $rows;
}

//Returns a list of rows containing a list of approved images for a given publish_date
function dbGetImageInfosByPublishDate($publishDate) {
    global $conn;
    dbConnect();
    
    $rows = array();
    if ( $result = mysql_query("select id, description, submit_date, source, email, status, publish_date, comment 
                                from images
                                where publish_date = '" . $publishDate . "' and status = 2", $conn) or die(mysql_error()) ){
        for ( $i = 0; $i < mysql_num_rows($result); $i++ ){
            $row = mysql_fetch_array( $result );
            $rows[$i] = $row;
        }
    }

    return $rows;
}

//Returns a list of rows containing a list of approved images for a given publish_date
function dbGetApprovedImageInfos($orderByClause, $maxRowsClause) {
    global $conn;
    dbConnect();
    
    $sql = "select id, description, submit_date, source, email, status, publish_date, comment 
            from images
            where status = 2 " . $orderByClause . " " . $maxRowsClause;

//print "<br><br>$sql<br><br>";

    $rows = array();
    if ( $result = mysql_query($sql, $conn) or die(mysql_error()) ){
        for ( $i = 0; $i < mysql_num_rows($result); $i++ ){
            $row = mysql_fetch_array( $result );
            $rows[$i] = $row;
        }
    }

    return $rows;
}

//Returns max publish date
function dbGetMaxPublishDate() {
    global $conn;
    dbConnect();
    
    if ( $result = mysql_query("select max(publish_date) from images", $conn) or die(mysql_error()) ){
        $row = mysql_fetch_array( $result );
        return $row[0];
    }

    return $rows;
}

//Returns the newly inserted row id or false
function dbInsertNewImageInfo($row) {
    global $conn;
    dbConnect();

    $sql = 'insert into images (description, submit_date, source, email, status, publish_date, image, comment) values ("' .
                                $row['description'] . '", "' . $row['submit_date'] . '", "' . $row['source'] . '", "' . $row['email'] . '", ' .
                                $row['status'] . ', "' . $row['publish_date'] . '", "' . $row['image'] . '", "' . $row['comment'] . '")';

    //print "<br>" . $sql . "<br>";
    if ( $result = mysql_query($sql, $conn) or die( mysql_error()) ){
        return $result;
    }

    return false;
}

//Returns true on success or false on failure
function dbUpdateImageInfo($row) {
    global $conn;
    dbConnect();

    $sql = 'update images set description = "' . $row['description'] . '", submit_date = "' . $row['submit_date'] . '", source = "' . $row['source'] . 
                                '", email = "' . $row['email'] . '", status = ' . $row['status'] . ', publish_date = "' . $row['publish_date'] . 
                                '", comment = "' . $row['comment'] . '" where id = ' . $row['id'];
    print '<br>' . $sql . '<br>';
    if ( $result = mysql_query($sql, $conn) or die( mysql_error()) ){
        return $result;
    }

    return false;
}

//Returns true on success or false on failure
function dbUpdateImageById($id, $img) {
    global $conn;
    dbConnect();

    $sql = 'update images set image = "' . $img . '" where id = ' . $id;
    //error_log("ROTATE02 - DB - '" . $id . "'");

    if ( $result = mysql_query($sql, $conn) or die( mysql_error()) ){
        return $result;
    }

    return false;
}

?>
